﻿#ifndef YCBASESERVICEH
#define YCBASESERVICEH
#include "request.h"
#include <QObject>
#include <QUrl>

namespace yc {

class RequestQueue;

class Service : public QObject
{
    Q_OBJECT

public:
    explicit Service(QObject* parent = nullptr);
    virtual ~Service();

    QNetworkAccessManager* networkAccessManager() const;
    const RequestHeaders& headers() const { return headers_; }
    RequestHeaders& headers() { return headers_; }
    void setRequestHeaders(const RequestHeaders& headers) { headers_ = headers; }

    void sendRequest(const Request request, const ResponseHandler& handler, bool logout = false,
                     int timeout = 15000);
    void cancelRequest(int seq);

    static int getNextSeq();
    static void setDownloadSpeed(qreal speed);
    static void setUploadSpeed(qreal speed);
    static qint64 downloadCostMSecs(qint64 bytes_total);
    static qint64 uploadCostMSecs(qint64 bytes_total);
    // 设置请求
    static void setupRequest(QNetworkRequest& request, const QString& url,
                             const QString& content_type);
    static void setupRequest(QNetworkRequest& request, const QString& url,
                             const RequestHeaders& headers, const QString& content_type);

Q_SIGNALS:
    void authenticationRequired();

private:
    RequestHeaders headers_;
    RequestQueue* queue_;
};

} // namespace yc

#endif // YCBASESERVICEH
